﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;

namespace TestConsoleApp
{
    public class TestClassCtor
    {
        public TestClassCtor()
        {
            var foo = (Foo1)RuntimeHelpers.GetUninitializedObject(typeof(Foo1));
            //foo = (Foo1)FormatterServices.GetUninitializedObject(typeof(Foo1));
            foo.F1 = 2;
            foo.F2 = 2;

            Console.WriteLine("F1:" + foo.F1 + "--F2:" + foo.F2);


            //var constructorInfo = typeof(Foo1).GetConstructor(new Type[2] { typeof(int), typeof(int) });
            //constructorInfo!.Invoke(foo, new object[] { 1, 2 });
            //Console.WriteLine("F1:" + foo.F1 + "--F2:" + foo.F2);
            //foo.F1 = 5;
            //foo.F2 = 5;
            //Console.WriteLine("F1:" + foo.F1 + "--F2:" + foo.F2);

            //constructorInfo!.Invoke(foo, new object[] { 1, 2 });
            //Console.WriteLine("F1:" + foo.F1 + "--F2:" + foo.F2);



            var constructorInfo = typeof(Foo1).GetConstructor(new Type[0]);
            constructorInfo!.Invoke(foo, null);
            foo.F1 = 5;
            foo.F2 = 5;
            var showMethod = typeof(Foo1).GetMethod("Show");
            showMethod?.Invoke(foo, null);

            //说明一个问题成员变量和属性的初始化，实际上是在构造函数内初始化
            constructorInfo!.Invoke(foo, null);
            showMethod = typeof(Foo1).GetMethod("Show");
            showMethod?.Invoke(foo, null);

            Foo1? foo1 = Find();

            Console.WriteLine($"Found {foo1!.Name}");
        }


        public Foo1 Find()
        {
            return null;
        }
    }

    public class Foo1 : IDisposable
    {
        public Foo1()
        {
            Console.WriteLine("Foo1:ctor");
        }

        public Foo1(int f1, int f2)
        {
            F1 = f1;
            F2 = f2;
            Console.WriteLine("Foo1:ctorByparm");
        }




        //public Foo1(string name)
        //{
        //    Name = name ?? throw new ArgumentNullException(nameof(name));
        //}

        public string Name { get; }

        public int F1 = 10;
        public int F2 { set; get; } = 10;

        public void Dispose()
        {
        }

        public void Show()
        {
            Console.WriteLine("F1:" + F1 + "--F2:" + F2);

        }

    }
}
